package com.PengGoBook.main.biz;

import android.app.Activity;
import android.util.Log;
import com.PengGoBook.main.BooksCategoryListEntity;
import com.PengGoBook.main.entity.*;
import com.PengGoBook.main.utils.Constants;
import com.PengGoBook.main.utils.RequestCacheUtil;
import com.PengGoBook.main.utils.Urls;
import com.PengGoBook.main.utils.Utility;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.type.TypeReference;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: S7
 * Date: 13-5-28
 * Time: 上午1:28
 * To change this template use File | Settings | File Templates.
 */
public class TopDao extends BaseDao {

    private BooksCategoryListEntity newsCategorys;
    private BlogsCategoryListEntity blogsCategorys;
    private WikiCategoryListEntity wikiCategorys;

    List<CategorysEntity> tabs = new ArrayList<CategorysEntity>();

    public TopDao(Activity activity) {
        super(activity);
    }

    public List<Object> mapperJson(boolean useCache) {
        List<Object> topCategorys = new ArrayList<Object>();
        tabs.clear();
        try {
           /* String resultNews = RequestCacheUtil.getRequestContent(mActivity,
                    Urls.BASIC_URL + Utility.getScreenParams(mActivity),
                    Constants.WebSourceType.Json,
                    Constants.DBContentType.Content_list, useCache);*/
            String resultNews = RequestCacheUtil.getRequestContent(mActivity,
                    Urls.BASIC_URL,
                    Constants.WebSourceType.Json,
                    Constants.DBContentType.Content_list, useCache);
            Log.i("info_out","json:"+resultNews);
            /*ObjectMapper  json turn into bean*/
            /*ignore field which don't have*/
            mObjectMapper.getDeserializationConfig().set(org.codehaus.jackson.map.DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            BooksCategoryListEntity booksMoreResponse = mObjectMapper.readValue(
                    resultNews, new TypeReference<BooksCategoryListEntity>() {
            });
            if (booksMoreResponse != null) {
                this.newsCategorys = booksMoreResponse;
                Log.i("info_out",this.newsCategorys.getCollections().size()+"大小");
            }
          /*  String resultBlogs = RequestCacheUtil.getRequestContent(mActivity,
                    Urls.BASIC_URL + Utility.getScreenParams(mActivity),
                    Constants.WebSourceType.Json,
                    Constants.DBContentType.Content_list, useCache);
            BlogsMoreResponse blogsMoreResponse = mObjectMapper.readValue(
                    resultBlogs, new TypeReference<BlogsMoreResponse>() {
            });
            if (blogsMoreResponse != null) {
                this.blogsCategorys = blogsMoreResponse.getResponse();
            }
            String resultWiki = RequestCacheUtil.getRequestContent(mActivity,
                    Urls.BASIC_URL + Utility.getScreenParams(mActivity),
                    Constants.WebSourceType.Json,
                    Constants.DBContentType.Content_list, useCache);
            WikiMoreResponse wikiMoreResponse = mObjectMapper.readValue(
                    resultWiki, new TypeReference<WikiMoreResponse>() {
            });
            if (wikiMoreResponse != null) {
                this.wikiCategorys = wikiMoreResponse.getResponse();
            }*/
            Collections.addAll(topCategorys, newsCategorys, blogsCategorys,
                    wikiCategorys);

            return topCategorys;
        } catch (JsonParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JsonMappingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;

    }

    public List<CategorysEntity> getCategorys() {
        CategorysEntity cate1 = new CategorysEntity();
        CategorysEntity cate2 = new CategorysEntity();
        CategorysEntity cate3 = new CategorysEntity();
        cate1.setName("精选资讯");
        cate2.setName("精选博客");
        cate3.setName("精选教程");
        tabs.add(cate1);
        tabs.add(cate2);
        tabs.add(cate3);
        return tabs;
    }

}
